
import numpy as np

# 使用arange()创建一个2x3数组，并使用nditer对它进行迭代
def fun1():
    a = np.arange(6).reshape(2, 3)
    print('原始数组是：')
    print(a)
    print("\n")
    print('迭代输出元素：')
    for x in np.nditer(a):
        print(x, end=", ")
    print("\n")

# 以 C 顺序访问数组转置的 copy
def fun2():
    a = np.arange(6).reshape(2, 3)
    for x in np.nditer(a.T):
        print(x, end=", ")
    print('\n')

    for x in np.nditer(a.T.copy(order='C')):
        print(x, end=', ')
    print('\n')

# 控制遍历顺序
def fun3():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print('原始数组是：')
    print(a)
    print('\n')

    print('原始数组的转置是：')
    b = a.T
    print(b)
    print('\n')

    print('以C风格顺序排序：')
    c = b.copy(order='C')
    print(c)
    for x in np.nditer(c):
        print(x, end=', ')
    print('\n')

    print('以F风格顺序排序：')
    c = b.copy(order='F')
    print(c)
    for x in np.nditer(c):
        print(x, end=', ')

def fun4():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print('原始数组是：')
    print(a)
    print('\n')

    print('以C风格顺序排序：')
    for x in np.nditer(a, order='C'):
        print(x, end=', ')
    print('\n')

    print('以F风格顺序排序：')
    for x in np.nditer(a, order='F'):
        print(x, end=', ')

# 修改数组中元素的值
def fun5():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print('原始数组是：')
    print(a)
    print('\n')

    for x in np.nditer(a, op_flags=['readwrite']):
        x[...] = 2 * x
    print('修改后的数组是：')
    print(a)

# 使用外部循环
def fun6():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print('原始数组是：')
    print(a)
    print('\n')
    for x in np.nditer(a, flags=['external_loop'], order='F'):
        print(x, end=', ')

# 广播迭代
def fun7():
    a = np.arange(0, 60, 5)
    a = a.reshape(3, 4)
    print('第一个数组为：')
    print(a)
    print('\n')

    b = np.array([1, 2, 3, 4], dtype=int)
    print(b)
    print('\n')
    print('修改后的数组为：')
    for x,y in np.nditer([a, b]):
        print("%d:%d" % (x, y), end=', ')

if __name__ == '__main__':
    fun7()